<?php
require_once("php/Field.php");
require_once ("php/general.php");

$update_flags = update_rows();

$table = "";
if (isset ($_GET["table"])) {
	$table = $_GET["table"];
}

list_tables($table);

if ($table == "") {
	exit;
}

$query = "select * from $table";
$result = mysql_query($query);

$hidden_fields = array (
	"id" => 1
);

$fields = array ();

$num_fields = mysql_num_fields($result);
for ($i = 0; $i < $num_fields; $i++) {
	$name = mysql_field_name($result, $i);
	$type = mysql_field_type($result, $i);
	$length = mysql_field_len($result, $i);
	$fields[] = new Field($name, $type, $length);
}

$num_rows = mysql_num_rows($result);

echo "<form method=post>\n";
echo "<input type=hidden name=table value=\"$table\"/>\n";
echo "<input type=hidden name=num_rows value=\"$num_rows\"/>\n";
// format field names
echo "<table cellspacing=6 cellpadding=4 align=center>";

echo "<tr>";
for ($i = 0; $i < count($fields); $i++) {
	$field_name = $fields[$i]->get_name();
	if (is_hidden_field($field_name)) {
		continue;
	}
	echo "<th align=left>$field_name</th>";
}
echo "</tr>";

$row_number = 0;
while ($row = mysql_fetch_assoc($result)) {
	$row_colour = $row_number % 2 == 0 ? "lightgreen" : "lightblue";
	echo "<tr bgcolor=\"$row_colour\">";

	for ($i = 0; $i < count($fields); $i++) {
		$field = $fields[$i];
		$field_name = $field->get_name();
		$field_type = $field->get_type();
		$field_length = $field->get_length();
		$field_value = $row[$field_name];
		//echo "<td>$field_value</td>\n";

		$form_name = "rows[${field_name}][]";

		if (is_hidden_field($field_name)) {
			echo "<input type=hidden name=\"$form_name\" value=\"$field_value\" />";
		}
		else {
			echo "<td>\n";
			echo "<input type=text name=\"$form_name\" value=\"$field_value\" size=\"$field_length\"/>";
			echo "</td>\n";
		}

		// HACK: no guarantee that row numbers the same... Should do based on id but need to ensure this is
		// consistent across all tables
	}

	if ($update_flags && $update_flags[$row_number]) {
		echo "<td bgcolor=yellow>Updated</td>";
	}

	echo "</tr>";
	$row_number++;
}

echo "<tr><td>";
echo "<input type=submit value=\"Update\"/>\n";
echo "</td></tr>";

echo "</table>";

echo "</form>";

$table = "course";
$result = mysql_query("SHOW COLUMNS FROM $table");

$column_names = array ();
while ($row = mysql_fetch_assoc($result)) {
	$column_names[] = $row["Field"];
}

function is_hidden_field($field_name) {
	global $hidden_fields;
	return isset ($hidden_fields[$field_name]);
}

function list_tables($current_table) {
	echo "<table cellpadding=10><tr>";
	//$self = $_SERVER["PHP_SELF"];
	$result = mysql_query("show tables");
	while ($row = mysql_fetch_row($result)) {
		$table = $row[0];
		$bgcolor = $table == $current_table ? "lightgreen" : "white";
		echo "<td bgcolor=\"$bgcolor\"><a href=\"?table=$table\">$table</a></td>";
	}
	echo "</tr></table>";
}

// use post data to update table
// hack: for moment, assume 'where' clause is always driven by 'id' field
function update_rows() {
	if (!isset ($_POST["table"]))
		return false;

	$table = $_POST["table"];
	$num_rows = $_POST["num_rows"];

	$rows = $_POST["rows"];
	$field_names = array_keys($rows);

	$update_flags = array ();
	for ($i = 0; $i < $num_rows; $i++) {
		$set_strings = array ();
		foreach ($field_names as $field_name) {
			$field_value = $rows[$field_name][$i];
			$field_value = mysql_escape_string($field_value);
			$set_strings[] = "$field_name=\"$field_value\"";
		}
		// HACK with query - always do with id???
		$id = mysql_escape_string($rows["id"][$i]);
		$query = "update $table set " . join(", ", $set_strings) . " where id=\"$id\"";
		$result = mysql_query($query);
		$updated = mysql_affected_rows();
		$update_flags[$i] = $updated;
	}

	return $update_flags;
}
?>
